﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace DigiCardDeck
{
    public partial class Details : System.Web.UI.Page
    {
        private string stConnectionString = string.Empty;
        private const string DECK_DB = "deckdb";
        private string deckNo = string.Empty;
        private string id = string.Empty;

        protected string myTitle;

        protected void Page_Load(object sender, EventArgs e)
        {
            deckNo = Request.QueryString["deckNo"];
            id = Request.QueryString["value"];
            // 接続文字列を生成する
            stConnectionString += "Data Source         = " + CodeConst.DATA_SOURCE + ";";
            stConnectionString += "Initial Catalog     = " + CodeConst.INITIAL_CATALOG + ";";
            //stConnectionString += "Integrated Security = SSPI;";
            stConnectionString += "User ID             = " + CodeConst.USER_ID + ";";
            stConnectionString += "Password            = " + CodeConst.PASSWORD + ";";

            
            // SqlConnection の新しいインスタンスを生成する (接続文字列を指定)
            System.Data.SqlClient.SqlConnection cSqlConnection = (
                new System.Data.SqlClient.SqlConnection(stConnectionString)
            );

            try
            {
                // データベース接続を開く
                cSqlConnection.Open();

                // cSqlConnection から SqlCommand のインスタンスを生成する
                System.Data.SqlClient.SqlCommand hCommand = cSqlConnection.CreateCommand();

                hCommand.CommandText = "select * from deckdb where ID = '" + id + "' and  deckno = " + deckNo;


                // 指定した SQL コマンドを実行する
                System.Data.SqlClient.SqlDataReader cReader = hCommand.ExecuteReader();

                if (!cReader.HasRows)
                {
                    hCommand.CommandText = "select * from deckdb where ID = '" + Server.UrlDecode( id ) + "' and  deckno = " + deckNo;
                    cReader.Dispose();
                    cReader = hCommand.ExecuteReader();
                }

                DataTable table = new DataTable();
                table.TableName = "deckViewer";
                DataSet dataSet = new DataSet();

                table.Columns.Add("image1");
                table.Columns.Add("image2");
                table.Columns.Add("image3");
                table.Columns.Add("image4");
                table.Columns.Add("image5");

                dataSet.Tables.Add(table);

                while (cReader.Read())
                {
                    Label1.Text = cReader["DeckName"].ToString();
                    Label2.Text = cReader["Remarks"].ToString() + "［" + cReader["ID"].ToString() + "]";

                    DataRow row = dataSet.Tables[0].NewRow();
                    row[0] = GetUrlValue(cReader["url1"].ToString());
                    row[1] = GetUrlValue(cReader["url2"].ToString());
                    row[2] = GetUrlValue(cReader["url3"].ToString());
                    row[3] = GetUrlValue(cReader["url4"].ToString());
                    row[4] = GetUrlValue(cReader["url5"].ToString());
                    dataSet.Tables[0].Rows.Add(row);

                    DataRow row2 = dataSet.Tables[0].NewRow();
                    row2[0] = GetUrlValue(cReader["url6"].ToString());
                    row2[1] = GetUrlValue(cReader["url7"].ToString());
                    row2[2] = GetUrlValue(cReader["url8"].ToString());
                    row2[3] = GetUrlValue(cReader["url9"].ToString());
                    row2[4] = GetUrlValue(cReader["url10"].ToString());
                    dataSet.Tables[0].Rows.Add(row2);

                    DataRow row3 = dataSet.Tables[0].NewRow();
                    row3[0] = GetUrlValue(cReader["url11"].ToString());
                    row3[1] = GetUrlValue(cReader["url12"].ToString());
                    row3[2] = GetUrlValue(cReader["url13"].ToString());
                    row3[3] = GetUrlValue(cReader["url14"].ToString());
                    row3[4] = GetUrlValue(cReader["url15"].ToString());
                    dataSet.Tables[0].Rows.Add(row3);


                    DataRow row4 = dataSet.Tables[0].NewRow();
                    row4[0] = GetUrlValue(cReader["url16"].ToString());
                    row4[1] = GetUrlValue(cReader["url17"].ToString());
                    row4[2] = GetUrlValue(cReader["url18"].ToString());
                    row4[3] = GetUrlValue(cReader["url19"].ToString());
                    row4[4] = GetUrlValue(cReader["url20"].ToString());
                    dataSet.Tables[0].Rows.Add(row4);

                    DataRow row5 = dataSet.Tables[0].NewRow();
                    row5[0] = GetUrlValue(cReader["url21"].ToString());
                    row5[1] = GetUrlValue(cReader["url22"].ToString());
                    row5[2] = GetUrlValue(cReader["url23"].ToString());
                    row5[3] = GetUrlValue(cReader["url24"].ToString());
                    row5[4] = GetUrlValue(cReader["url25"].ToString());
                    dataSet.Tables[0].Rows.Add(row5);

                    DataRow row6 = dataSet.Tables[0].NewRow();
                    row6[0] = GetUrlValue(cReader["url26"].ToString());
                    row6[1] = GetUrlValue(cReader["url27"].ToString());
                    row6[2] = GetUrlValue(cReader["url28"].ToString());
                    row6[3] = GetUrlValue(cReader["url29"].ToString());
                    row6[4] = GetUrlValue(cReader["url30"].ToString());
                    dataSet.Tables[0].Rows.Add(row6);
                }



                GridView1.DataSource = dataSet;
                GridView1.DataBind();
                GridView1.DataMember = dataSet.Tables[0].TableName;

                cReader.Dispose();

                hCommand.Dispose();

                Page.Header.Title = "デック詳細：" + Label1.Text;
            }
            catch (Exception ex)
            {

            }
            finally
            {
                // データベース接続を閉じる
                cSqlConnection.Close();
                cSqlConnection.Dispose();

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //Label1.Text = Request.QueryString["value"]; 
        }


        private string GetUrlValue(string url)
        {
            return @"http://digitalmado.com/card/img/" + url + "/card.jpg";

        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //6Label1.Text = "action!";
        }

        protected void DropDownZoom_SelectedIndexChanged(object sender, EventArgs e)
        {
            var dropDownList = ( DropDownList )sender;

            Page.Form.Style[ "zoom" ] = dropDownList.SelectedValue;
        }
    }
}